package com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget

import android.content.Context
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hhsjdesign.dynamicui.annoprocess.AWidgetType
import com.hhsjdesign.dynamicui.core.widgetimpl.BusinessWidget
import com.hhsjdesign.dynamicui.interfaces.RequestListener
import com.hhsjdesign.dynamicui.businessdata.BaseModel
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.core.property.ShapeProperty
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.property.widget.GridHWidgetProperty
import com.hhsjdesign.dynamicui.core.property.WidgetSpecProperty
import com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget.adapter.RecyclerSimpleAdapter
import com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget.adapter.WidgetHolder
import com.hhsjdesign.dynamicui.core.property.viewdata.ItemViewData
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetTypeConst

//HorizontalGridLayoutProperty
@AWidgetType(WidgetTypeConst.GRID_HORIZONTAL)
class GridHWidget(
    context: Context,
    data: WidgetData
) : BusinessWidget(context, data) {

    lateinit var recyclerView: RecyclerView
    var adapter: RecyclerSimpleAdapter? = null

    override fun createMainView(specProperty: WidgetSpecProperty): View {
        recyclerView = RecyclerView(context)
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.layoutManager =
            GridLayoutManager(
                context,
                (widgetData.specProperty as GridHWidgetProperty).rowNumber,
                GridLayoutManager.HORIZONTAL,
                false
            ).apply {
            }
        return recyclerView.apply {
            addItemDecoration(object : RecyclerView.ItemDecoration() {

                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    super.getItemOffsets(outRect, view, parent, state)

                    //space
                    (widgetData.specProperty as GridHWidgetProperty).apply {
                        if (parent.getChildAdapterPosition(view) / this.rowNumber == 0) {
                            outRect.left = 0
                            outRect.top = this.verticalSpace
                            outRect.right = this.horizontalSpace
                            outRect.bottom = this.verticalSpace
                        } else if ((parent.getChildAdapterPosition(view) + 1) / this.rowNumber == 0) {
                            outRect.left = this.horizontalSpace
                            outRect.top = this.verticalSpace
                            outRect.right = 0
                            outRect.bottom = this.verticalSpace
                        } else {
                            outRect.left = this.horizontalSpace
                            outRect.top = this.verticalSpace
                            outRect.right = this.horizontalSpace
                            outRect.bottom = this.verticalSpace
                        }
                    }

                }
            })
        }
    }

    override fun layout(property: ShapeProperty, parentType: String) {
        super.layout(property, parentType)
    }

    override fun loadLocalBusiness() {
        if (adapter == null) {
            adapter = HorizontalGridAdapter(
                context,
                widgetData.business!!,
                widgetData.itemPropertyList!![0].itemWidgetType,
                widgetData.itemPropertyList!![0].itemPropertyModel,
                widgetData.businessData!!,
                (widgetData.specProperty as GridHWidgetProperty).itemWidth,
                (widgetData.specProperty as GridHWidgetProperty).itemHeight,
                widgetData.viewProperty!!.size[3]
            )
        }
        setAdapter()
    }

    override fun loadRemoteBusiness() {
        dataCaller?.loadData(
            widgetData.business!!,
            widgetData.params!!,
            this,
            object : RequestListener<Any> {
                override fun onRequestSuccess(requestId: String, result: BaseModel<Any>) {
                    if (result.type == 1) {
                        HorizontalGridAdapter(
                            context,
                            widgetData.business!!,
                            widgetData.itemPropertyList!![0].itemWidgetType,
                            widgetData.itemPropertyList!![0].itemPropertyModel,
                            result.toIViewableList() as MutableList<IViewable<*>>,
                            (widgetData.specProperty as GridHWidgetProperty).itemWidth,
                            (widgetData.specProperty as GridHWidgetProperty).itemHeight,
                            widgetData.viewProperty!!.size[3]//父布局如果是-2
                        ).let {
                            recyclerView.adapter = it
                            it.notifyDataSetChanged()
                        }
                    }
                }

                override fun onRequestFail(
                    requestId: String,
                    resultCode: Int,
                    resultMessage: String
                ) {
                }
            }
        )
    }

    private fun setAdapter() {
        if (adapter != null && recyclerView != null) {
            recyclerView.adapter = adapter
        }
    }

    open class HorizontalGridAdapter constructor(
        context: Context,
        business: String,
        itemWidgetType: String?,
        viewWidget: ItemViewData?,
        itemDataList: MutableList<IViewable<*>>,
        itemWidth: Int,
        itemHeight: Int,
        parentHeightType: Int
    ) : RecyclerSimpleAdapter(
        context,
        business,
        itemWidgetType,
        viewWidget,
        itemDataList,
        itemWidth,
        itemHeight,
        parentHeightType
    ) {

        override fun onBindViewHolder(holder: WidgetHolder<*>, position: Int) {
            super.onBindViewHolder(holder, position)
            //horizontal 的 宽度需要手动设定
            holder.itemView.layoutParams.width = itemWidth
            //如果父布局高度是wrap_content 将 用参数高度去设置
            if (parentHeightType == -2) {
                holder.itemView.layoutParams.height = itemHeight
            }
        }
    }

}